//
//  HomeViewController.m
//  ReciteBook
//
//  Created by xuziyuan on 13-12-20.
//  Copyright (c) 2013年 student. All rights reserved.
//

#import "HomeViewController.h"
#import "LoginViewController.h"
#import "ContentViewController.h"
#import "Content.h"

@interface HomeViewController ()

@property (nonatomic,weak) UITableViewCell *lastCell;

@property (nonatomic,strong) UILabel *ss;
@end

@implementation HomeViewController

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void) viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden=NO;
    [self queryContent];
    [self compare];
    [self.tableView reloadData];
    
    NSLog(@"出现后名字是%@",self.name);
    if (self.name.length>0) {
        UIBarButtonItem *rightBtn=[[UIBarButtonItem alloc] initWithTitle:self.name style:UIBarButtonItemStylePlain target:self action:@selector(entrys)];
        self.navigationItem.rightBarButtonItem=rightBtn;
    }  
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.myAppDelegate=(AppDelegate *)[[UIApplication sharedApplication] delegate];
    self.tableView.scrollEnabled=NO;
//    [self queryContent];
//    [self compare];
    
    self.scrollV=[[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 160)];
    self.scrollV.delegate=self;
    self.scrollV.contentSize=CGSizeMake(320*3, 160);
    self.scrollV.pagingEnabled=YES;
    self.scrollV.showsHorizontalScrollIndicator=NO;
    self.scrollV.showsVerticalScrollIndicator=NO;
    
    UIImage *img1=[UIImage imageNamed:@"tangshi"];
    UIImageView *imgView1=[[UIImageView alloc] initWithImage:img1];
    imgView1.frame=CGRectMake(320*0, 0, 320, 160);
    [self.scrollV addSubview:imgView1];
    
    UIImage *img2=[UIImage imageNamed:@"songci"];
    UIImageView *imgView2=[[UIImageView alloc] initWithImage:img2];
    imgView2.frame=CGRectMake(320*1, 0, 320, 160);
    [self.scrollV addSubview:imgView2];
    
    UIImage *img3=[UIImage imageNamed:@"guwen"];
    UIImageView *imgView3=[[UIImageView alloc] initWithImage:img3];
    imgView3.frame=CGRectMake(320*2, 0, 320, 160);
    [self.scrollV addSubview:imgView3];
    
    self.tableView.tableHeaderView=self.scrollV;
    
    UIPageControl *pageControl=[[UIPageControl alloc] initWithFrame:CGRectMake(0, 130, 320, 30)];
    pageControl.numberOfPages=3;
    pageControl.tag=101;
    
    [self.view addSubview:pageControl];
    
    UIBarButtonItem *rightBtn=[[UIBarButtonItem alloc] initWithTitle:@"登录" style:UIBarButtonItemStylePlain target:self action:@selector(entrys)];
    self.navigationItem.rightBarButtonItem=rightBtn;

}
//获取数据库数据，并存储进数组list
- (void)queryContent
{
    NSEntityDescription *entityContent=[NSEntityDescription entityForName:@"Content" inManagedObjectContext:self.myAppDelegate.managedObjectContext];
    NSFetchRequest *request=[[NSFetchRequest alloc] init];
    
    [request setEntity:entityContent];
    self.list=[[self.myAppDelegate.managedObjectContext executeFetchRequest:request error:nil] mutableCopy];
    
//    for (Content *temp in self.list) {
//        NSLog(@"体裁=%@,题目=%@,作者=%@",temp.sort,temp.title,temp.author);
//    }
}
//获取唯一的体裁，并放入数组listSort
- (void)compare
{
    self.listSort=[[NSMutableArray alloc] init];
    for (Content *temp in self.list) {
        if (self.listSort.count==0) {
            [self.listSort addObject:temp.sort];
        }
        int flag=0;
        for (NSString *temp1 in self.listSort) {
            if ([temp.sort isEqualToString:temp1]) {
                flag=1;
                break;
            }
        }
        if (flag==0) {
            [self.listSort addObject:temp.sort];
        }
    }
    
    for (NSString *temp in self.listSort) {
        NSLog(@"体裁=%@",temp);
    }
    NSLog(@"---------");
    
}
//查询指定体裁的内容
- (NSMutableArray *)queryAppointContent:(NSString *)sort
{
    NSPredicate *pre=[NSPredicate predicateWithFormat:@"sort=%@",sort];
    NSMutableArray *arr=[NSMutableArray arrayWithArray:[self.list filteredArrayUsingPredicate:pre]];
    if (arr.count==0) {
        return nil;
    }else{
        return arr;
    }
    
}

- (void)entrys
{
    UIAlertView *alert=[[UIAlertView alloc] initWithTitle:nil message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"登录", @"注册",nil];
    [alert show];

}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex==1) {
        EntryViewController *entry=[[EntryViewController alloc] initWithNibName:@"EntryViewController" bundle:nil];
        entry.delegate=self;
        [self.navigationController pushViewController:entry animated:YES];
        
    }else if (buttonIndex==2){
        LoginViewController *login=[[LoginViewController alloc] init];
        [self.navigationController pushViewController:login animated:YES];
    }
}





- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.listSort.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if (cell==nil) {
        cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    
    cell.textLabel.text=self.listSort[indexPath.row];
    
    return cell;
}



#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    NSInteger row=indexPath.row;
    [self.scrollV setContentOffset:CGPointMake(320*row, 0) animated:YES];
    
    UIPageControl *tmpControl=(UIPageControl *)[self.view viewWithTag:101];
    tmpControl.currentPage=row;
    
    UITableViewCell *tmpCell=[tableView cellForRowAtIndexPath:indexPath];
    if (self.lastCell==nil) {
        tmpCell.accessoryType=UITableViewCellAccessoryDetailDisclosureButton;
        self.lastCell=tmpCell;
    }
    if (self.lastCell==tmpCell) {
        tmpCell.accessoryType=UITableViewCellAccessoryDetailDisclosureButton;
    }else{
        tmpCell.accessoryType=UITableViewCellAccessoryDetailDisclosureButton;
        self.lastCell.accessoryType=UITableViewCellAccessoryNone;
        self.lastCell=tmpCell;
    }
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if ([scrollView isMemberOfClass:[UIScrollView class]]) {
        UIPageControl *tmpControl=(UIPageControl *)[self.view viewWithTag:101];
        tmpControl.currentPage=self.scrollV.contentOffset.x/320;
        
        NSIndexPath *tmpIndexPath=[NSIndexPath indexPathForRow:tmpControl.currentPage inSection:0];
        [self.tableView selectRowAtIndexPath:tmpIndexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
        
        
        UITableViewCell *tmpCell=[self.tableView cellForRowAtIndexPath:tmpIndexPath];
        if (self.lastCell==nil) {
            tmpCell.accessoryType=UITableViewCellAccessoryDetailDisclosureButton;
            self.lastCell=tmpCell;
        }
        if (self.lastCell==tmpCell) {
            tmpCell.accessoryType=UITableViewCellAccessoryDetailDisclosureButton;
        }else{
            tmpCell.accessoryType=UITableViewCellAccessoryDetailDisclosureButton;
            self.lastCell.accessoryType=UITableViewCellAccessoryNone;
            self.lastCell=tmpCell;
        }
    }
}

- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
    

    NSString *temp= self.listSort[indexPath.row];
    NSMutableArray *tempArr=[self queryAppointContent:temp];
    if (tempArr.count==0) {
        NSLog(@"空数组");
    }else{
        ContentViewController *contentVC=[[ContentViewController alloc] initWithNibName:@"ContentViewController" bundle:nil];
        contentVC.contentList=tempArr;
        [self.navigationController pushViewController:contentVC animated:YES];
    }

//        [[NSNotificationCenter defaultCenter] postNotificationName:@"changedHomeIdenPath" object:indexPath];
    
}

@end
